#!/usr/bin/python
# -*- coding:utf-8 -*-
import pandas as pd

# class ExcelFinder:
#     def __init__(self, data):
#         self.data = data
#
#     def find_data(self, wellDepth):
#         df = self.data
#
#         # 找到比目标值小的最近值
#         dMinList = df.query('井深 <= @wellDepth')
#         if not dMinList.empty:
#             min_row = dMinList.loc[dMinList['井深'].idxmax()]
#         else:
#             min_row = None
#
#         # 找到比目标值大的最近值
#         dMaxList = df.query('井深 >= @wellDepth')
#         if not dMaxList.empty:
#             max_row = dMaxList.loc[dMaxList['井深'].idxmin()]
#         else:
#             max_row = None
#
#         print(f"find_data: value={wellDepth}\n")  # 调试信息
#         return max_row, min_row

from PySide6.QtCore import Qt
class ExcelFinder:
    def __init__(self, data_table):
        self.data_table = data_table

    def find_data(self, wellDepth):
        min_row = None
        max_row = None

        for row in range(self.data_table.rowCount()):
            item = self.data_table.item(row, 0)  # 假设井深在第一列
            if item is not None:
                depth = float(item.data(Qt.UserRole))
                if depth <= wellDepth:
                    if min_row is None or depth > float(self.data_table.item(min_row, 0).text()):
                        min_row = row
                if depth >= wellDepth:
                    if max_row is None or depth < float(self.data_table.item(max_row, 0).text()):
                        max_row = row

        if min_row is not None:
            min_row_data = [self.data_table.item(min_row, col).data(Qt.UserRole) if self.data_table.item(min_row, col) else None for col in range(self.data_table.columnCount())]
        else:
            min_row_data = None

        if max_row is not None:
            max_row_data = [self.data_table.item(max_row, col).data(Qt.UserRole) if self.data_table.item(max_row, col) else None for col in range(self.data_table.columnCount())]
        else:
            max_row_data = None

        print(f"find_data: value={wellDepth}\n")  # 调试信息
        return max_row_data, min_row_data

